package iscript.uiFramework.components.button
{
	import flash.display.DisplayObject;
	import iscript.uiFramework.components.UIComponent;
	
	public class ButtonGroup 
	{
		private var _buttonList:Vector.<IButton>
		private var _prevPressButton:IButton;
		private var _currentPressButton:IButton;
		
		public function ButtonGroup()
		{
			_buttonList = new Vector.<IButton>();
		}
		
		public function append(but:IButton):void
		{
			_buttonList.push(but);
			but.ClickBeforeHandler = function(e:*):void
			{
				but.state = ButtonState.DOWN;
				
				if (_prevPressButton != null)
					_prevPressButton = _currentPressButton;
				_currentPressButton = but;
				if (_prevPressButton == null)
					_prevPressButton = _currentPressButton;
					
				IButton(but).enabled = false;
				_buttonGroupClickLogic(but);
				
			}
		}
		
		public function remove(but:IButton):void
		{
			var index:int = _buttonList.indexOf(but);
			if (index != -1)
			_buttonList.splice(index, 1);
		}
		
		private function _buttonGroupClickLogic(but:IButton):void
		{
			for each (var item:IButton in _buttonList)
			{
				if (item != but)
				{
					IButton(item).enabled = true;
					item.state = ButtonState.NORMAL;
				}
			}
			if (_prevPressButton != null && _prevPressButton != _currentPressButton)
				IButton(_prevPressButton).enabled = true;
		}
	}

}